package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_goods_p;

import cn.com.api.service.ApiT_goods_pService;

import cn.com.util.*;
import cn.com.util.*;

public class T_goods_pBaseController {

	private static Logger logger = Logger
			.getLogger(T_goods_pBaseController.class);

	@Autowired
	private ApiT_goods_pService apiT_goods_pService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_p-list")
	public ModelAndView t_goods_p_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_p_list.jsp");

		List<ApiT_goods_p> t_goods_ps = apiT_goods_pService.findT_goods_ps();
		mv.addObject("t_goods_ps", t_goods_ps);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_p-add-pre")
	public ModelAndView t_goods_p_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_goods_p_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_goods_p t_goods_p = apiT_goods_pService
					.findT_goods_pById(Integer.valueOf(id));
			mv.addObject("t_goods_p", t_goods_p);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_p-add")
	public void t_goods_p_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String goodsid = request.getParameter("goodsid");
		String filename = request.getParameter("filename");
		String path = request.getParameter("path");
		String note = request.getParameter("note");
		String commit_time = request.getParameter("commit_time");
		String status = request.getParameter("status");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_goods_p t_goods_p = new ApiT_goods_p();
			/*
			 * t_goods_p.setId(id); t_goods_p.setGoodsid(goodsid);
			 * t_goods_p.setFilename(filename); t_goods_p.setPath(path);
			 * t_goods_p.setNote(note); t_goods_p.setCommit_time(commit_time);
			 * t_goods_p.setStatus(status);
			 */
			apiT_goods_pService.insertT_goods_p(t_goods_p);
		}
		// edit
		else {
			ApiT_goods_p t_goods_p = apiT_goods_pService
					.findT_goods_pById(Integer.valueOf(id));
			/*
			 * t_goods_p.setId(id); t_goods_p.setGoodsid(goodsid);
			 * t_goods_p.setFilename(filename); t_goods_p.setPath(path);
			 * t_goods_p.setNote(note); t_goods_p.setCommit_time(commit_time);
			 * t_goods_p.setStatus(status);
			 */
			apiT_goods_pService.updateT_goods_p(t_goods_p);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_p-delete")
	public void t_goods_p_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_goods_pService.deleteT_goods_p(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
